package com.wangsd.common.utils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JiGuangUtils {
    //极光ZengYangMei账号
    private static final String APPKEY = "3fa3375912c71f6c74968df5";
    private static final String MASTERSECRET = "98f0e761e5074871c070464f";
    public static JPushClient jpushClient = null;

    /**
     * 推送消息到指定设备
     *
     * @param registrationId 设备别名
     * @param title          标题
     * @param content        内容
     */
    public static void sendPushNotice(String registrationId, String title, String content) throws Exception {
        jpushClient = new JPushClient(MASTERSECRET, APPKEY);
        PushPayload payload = null;
        if (registrationId != null) {
            //推送指定別名
            payload = buldPushObject_alias(registrationId, title, content);
        }
        try {
            System.out.println(payload.toString());
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("推送结果：" + result.getResponseCode());
            log.info("Got result - " + result);
        } catch (APIConnectionException e) {
            throw new Exception("打印失败");
        } catch (APIRequestException e) {
            throw new Exception("打印失败");
        }
    }


    public static PushPayload buldPushObject_alias(String registrationId, String title, String content) {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder()
                                                .setAlert(content)
                                                .setTitle(title)
                                                .build())
                                .build()).build();
    }
}
